package com.crm.system.service.impl.sys;

import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompanyProject;
import com.crm.model.vo.sys.CompanyProjectTVO;
import com.crm.model.vo.sys.CompanyProjectVO;
import com.crm.service.sys.permissionManagement.CompanyProjectDbService;
import com.crm.system.service.sys.CompanyProjectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:chaifuyou
 * @Date: 2021/7/27 9:18
 */
@Service
public class CompanyProjectServiceImpl implements CompanyProjectService {

    @Autowired
    private CompanyProjectDbService companyProjectDbService;

    /**
     * 增加分公司项目
     * @param sysCompanyProject
     */
    @Override
    public void addCompanyProject(SysCompanyProject sysCompanyProject) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        sysCompanyProject.setCompanyProjectId(CommonUtils.getGUID());
        sysCompanyProject.setCreatePeopleId(userAccount.getAccountId());
        sysCompanyProject.setCreateTm(new Date());
        companyProjectDbService.addCompanyProject(sysCompanyProject);
    }

    /**
     * 分页查询分公司项目列表
     * @param pageIndex
     * @param pageSize
     * @param companyName
     * @return
     */
    @Override
    public DhPageInfo<CompanyProjectVO> selectCompanyProjectList(Integer pageIndex, Integer pageSize, String companyName,String companyId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<CompanyProjectVO> customerList = companyProjectDbService.selectCompanyProjectList(companyName,companyId);
        PageInfo<CompanyProjectVO> pageInfo = new PageInfo<CompanyProjectVO>(customerList);
        return new DhPageInfo<CompanyProjectVO>(pageInfo, customerList);
    }

    /**
     * 删除项目
     * @param companyProjectId
     */
    @Override
    public void delCompanyProject(String companyProjectId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        SysCompanyProject sysCompanyProject = new SysCompanyProject();
        sysCompanyProject.setDeleteStatus(1);
        sysCompanyProject.setCompanyProjectId(companyProjectId);
        sysCompanyProject.setUpdatePeopleId(userAccount.getAccountId());
        sysCompanyProject.setUpdateTm(new Date());
        companyProjectDbService.updateCompanyProject(sysCompanyProject);
    }

    /**
     * 修改分公司项目
     * @param sysCompanyProject
     */
    @Override
    public void updateCompanyProject(SysCompanyProject sysCompanyProject) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        sysCompanyProject.setUpdatePeopleId(userAccount.getAccountId());
        sysCompanyProject.setUpdateTm(new Date());
        companyProjectDbService.updateCompanyProject(sysCompanyProject);
    }

    /**
     * 分页查询公司项目列表（填充了一些数据）
     * @param pageIndex
     * @param pageSize
     * @param companyName
     * @param companyId
     * @return
     */
    @Override
    public DhPageInfo<CompanyProjectTVO> selectCompanyProjectListT(Integer pageIndex, Integer pageSize, String companyName, String companyId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<CompanyProjectVO> customerList = companyProjectDbService.selectCompanyProjectList(companyName,companyId);
        List<CompanyProjectTVO> list = new ArrayList<>();
        for (int i=0;i<customerList.size();i++){
            CompanyProjectTVO companyProjectTVO = new CompanyProjectTVO();
            companyProjectTVO.setCompanyId(customerList.get(i).getCompanyId());
            companyProjectTVO.setCompanyName(customerList.get(i).getCompanyName());
            companyProjectTVO.setCompanyProjectId(customerList.get(i).getCompanyProjectId());
            companyProjectTVO.setProjectName(customerList.get(i).getProjectName());
            companyProjectTVO.setTaxRate(customerList.get(i).getTaxRate());
            companyProjectTVO.setTaxClassNumber(customerList.get(i).getTaxClassNumber());
            list.add(companyProjectTVO);
        }
        PageInfo<CompanyProjectTVO> pageInfo = new PageInfo<CompanyProjectTVO>(list);
        return new DhPageInfo<CompanyProjectTVO>(pageInfo, list);
    }
}
